package com.ding.demo.excel;

import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.OutputStream;

@RequestMapping("excelExport")
@RestController
public class SXSSFWorkbookTest {


     @GetMapping("sXSSFWorkbook")
    String  sXSSFWorkbookExport(){

         long t1 = 0;long t2=0;
        try {
            t1 = System.currentTimeMillis();
            SXSSFWorkbook workbook = new SXSSFWorkbook();
            workbook.createSheet("aaa");
            SXSSFSheet aaa = workbook.getSheetAt(0);
            for (int i=0;i<100000;i++){
                aaa.createRow(i);
                aaa.getRow(i).createCell(0).setCellValue("悦宝");
                aaa.getRow(i).createCell(1).setCellValue("女孩");
                aaa.getRow(i).createCell(2).setCellValue("一岁");
                aaa.getRow(i).createCell(3).setCellValue("青岛");
            }
            OutputStream outputStream = null;
            // 打开目的输入流，不存在则会创建
            outputStream = new FileOutputStream("D:\\SXSSFWorkbook.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            t2 = System.currentTimeMillis();
            System.out.println("SXSSFWorkbook : 100w条数据写入Excel 消耗时间："+ (t2-t1));
        } catch (Exception e) {
            e.printStackTrace();
        }
      return "SXSSFWorkbook : 100w条数据写入Excel 消耗时间："+ (t2-t1);
    }

    @GetMapping("xSSFWorkbook")
    String  xSSFWorkbookExport(){


        long t1 = 0;long t2=0;
        try {
            t1 = System.currentTimeMillis();
            XSSFWorkbook workbook = new XSSFWorkbook();
            workbook.createSheet("aaa");
            XSSFSheet aaa = workbook.getSheetAt(0);
            for (int i=0;i<100000;i++){
                aaa.createRow(i);
                aaa.getRow(i).createCell(0).setCellValue("悦宝");
                aaa.getRow(i).createCell(1).setCellValue("女孩");
                aaa.getRow(i).createCell(2).setCellValue("一岁");
                aaa.getRow(i).createCell(3).setCellValue("青岛");
            }
            OutputStream outputStream = null;
            // 打开目的输入流，不存在则会创建
            outputStream = new FileOutputStream("D:\\XSSFWorkbook.xlsx");
            workbook.write(outputStream);
            outputStream.close();
            t2 = System.currentTimeMillis();
            System.out.println("XSSFWorkbook : 100w条数据写入Excel 消耗时间："+ (t2-t1));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "XSSFWorkbook : 100w条数据写入Excel 消耗时间："+ (t2-t1);
    }
}
